from neo4j import GraphDatabase
'''
对于刚安装好的neo4j库，应先使用cypher-shell.bat进行连结，并按照提示修改密码，
如果使用默认用户名密码neo4j/neo4j，则会报错
The credentials you provided were valid, but must be changed before you can use this instance. 
If this is the first time you are using Neo4j, this is to ensure you are not using the default credentials in production.
If you are not using default credentials, you are getting this message because an administrator requires a password change.
To change your password, issue an `ALTER CURRENT USER SET PASSWORD FROM 'current password' TO 'new password'` 
statement against the system database.}

如果希望不进行密码校验
在配置文件设置
dbms.security.auth_enabled=false

'''

#定义写函数，注意参数传递方法
def addOne(tx,name):
    tx.run("CREATE (a:Person {name:$name})",
           name=name)
#定义读函数，并进行结果显示
def getOne(tx,name):
    for record in tx.run("MATCH (a:Person) WHERE a.name = $name "
                         "RETURN a.name", name=name):
        print(record["a.name"])


#连接到服务，并切换数据库
driver = GraphDatabase.driver("bolt://localhost:7687",auth=("neo4j","12345678"))
session = driver.session(database="neo4j")
#以事务方式进行读写
with driver.session() as session:
    #session.write_transaction(addOne, "alice") #4.x写法
    #session.read_transaction(getOne, "alice")
    session.execute_write(addOne, "alice")#5.x写法
    session.execute_read(getOne, "alice")
driver.close()#关闭连接


'''
官方手册：https://neo4j.com/docs/python-manual/current/
较复杂的示例代码：https://github.com/neo4j-examples
'''